package com.example.plumneedle.Framgment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.plumneedle.PinLu.AdapterComment;
import com.example.plumneedle.PinLu.Comment;
import com.example.plumneedle.R;

import java.util.ArrayList;
import java.util.List;


public class BlankFragment4 extends Fragment implements View.OnClickListener  {

    private ImageView comment;
    private TextView hide_down;
    private EditText comment_content;
    private Button comment_send;
    private ConstraintLayout constraintLayout;

   // private LinearLayout rl_enroll;
    private RelativeLayout rl_comment;

    private ListView comment_list;
    private AdapterComment adapterComment;
    private List<Comment> data;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank4, container, false);
    }

    public void onActivityCreated(@Nullable Bundle saveInstanceState){
        super.onActivityCreated(saveInstanceState);
        initView();
    }
    private void initView() {

        // 初始化评论列表
        comment_list = (ListView) getActivity().findViewById(R.id.comment_list1);
        // 初始化数据
        data = new ArrayList<>();
        // 初始化适配器
        adapterComment = new AdapterComment(getContext(), data);
        // 为评论列表设置适配器
        comment_list.setAdapter(adapterComment);
        comment = (ImageView) getActivity().findViewById(R.id.imageView52);
        hide_down = (TextView) getActivity().findViewById(R.id.hide_down1);
        comment_content = (EditText) getActivity().findViewById(R.id.comment_content1);
        comment_send = (Button) getActivity().findViewById(R.id.comment_send1);
        constraintLayout=getActivity().findViewById(R.id.con);

       // rl_enroll = (LinearLayout) getActivity().findViewById(R.id.rl_enroll);
        rl_comment = (RelativeLayout) getActivity().findViewById(R.id.rl_comment1);

        setListener();
    }

    /**
     * 设置监听
     */
    public void setListener(){
        comment.setOnClickListener(this);

        hide_down.setOnClickListener(this);
        comment_send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView52:
                // 弹出输入法
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                // 显示评论框
               // rl_enroll.setVisibility(View.GONE);
                constraintLayout.setVisibility(View.GONE);
                rl_comment.setVisibility(View.VISIBLE);
                break;
            case R.id.hide_down1:
                // 隐藏评论框
            //    rl_enroll.setVisibility(View.VISIBLE);
                constraintLayout.setVisibility(View.VISIBLE);
                rl_comment.setVisibility(View.GONE);
                // 隐藏输入法，然后暂存当前输入框的内容，方便下次使用
                InputMethodManager im = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                im.hideSoftInputFromWindow(comment_content.getWindowToken(), 0);
                break;
            case R.id.comment_send1:
                sendComment();
                break;
            default:
                break;
        }
    }

    /**
     * 发送评论
     */
    public void sendComment(){
        if(comment_content.getText().toString().equals("")){
            Toast.makeText(getContext(), "评论不能为空！", Toast.LENGTH_SHORT).show();
        }else{
            // 生成评论数据
            Comment comment = new Comment();
            comment.setName("小强");
            comment.setContent(comment_content.getText().toString());
            adapterComment.addComment( comment);
            // 发送完，清空输入框
            comment_content.setText("");

            Toast.makeText(getContext(), "评论成功！", Toast.LENGTH_SHORT).show();
        }
    }
   /* class adapterComment extends ListView<MyListView> {
        public adapterComment(Context context) {
            super(context);
        }

        public adapterComment(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public adapterComment(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public adapterComment(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
    }*/
}
